package com.hzy.es.config;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class ESConfig {
    @Value("${spring.elasticsearch.rest.uris}")
    private String url;

    @Bean
    public JestClient getJestClient(){
        List<String> uris = Arrays.asList(url.split(","));
        JestClientFactory jestClientFactory = new JestClientFactory();
        jestClientFactory.setHttpClientConfig(new HttpClientConfig
                .Builder(uris)
                .connTimeout(10000)
                .readTimeout(10000)
                .maxConnectionIdleTime(1500L, TimeUnit.MILLISECONDS)
                .multiThreaded(true)
                .build());
        return jestClientFactory.getObject();
    }
}

